<template>
  <div class="repair">
    <div class="screen_box">
      <div class="select_box">
        <div class="select_li">
          <el-select v-model="yearData" placeholder="请选择" size="mini" @change="selectTime('year')" value-key="year">
            <el-option
              v-for="item in yearOption"
              :key="item.year"
              :label="item.year === '全部' ? '全部' : item.year + '年'"
              :value="item"></el-option>
          </el-select>
          <el-select v-model="monthData" placeholder="请选择" size="mini" @change="selectTime('month')">
            <el-option
              v-for="item in monthOption"
              :key="item.month"
              :label="item.month === '全部' ? '全部' : item.month + '月'"
              :value="item.month"></el-option>
          </el-select>
        </div>
        <div class="select_li" v-if="!['1', '2'].includes(userType)">
          <el-select v-model="orgData" placeholder="请选择" size="mini" @change="selectOrg" clearable>
            <el-option v-for="item in orgOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </div>
        <div class="select_li">
          <div>状态：</div>
          <el-select v-model="statusData" placeholder="请选择" size="mini" @change="selectStatus">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </div>
      </div>
      <div class="btn">
        <el-button type="primary" size="small" @click="exportData">导 出</el-button>
      </div>
    </div>
    <div class="info_con">
      <div class="info_right">
        <div class="table_con" ref="table_con">
          <vxe-table
            border
            stripe
            class="mytable-scrollbar"
            highlight-hover-row
            size="medium"
            height="100%"
            :data="tableData"
            align="center"
            v-if="!load">
            <vxe-table-column type="seq" title="序号" width="70"></vxe-table-column>
            <vxe-table-column field="userName" title="申请人" width="120"></vxe-table-column>
            <vxe-table-column field="orgName" title="申请部门" width="200"></vxe-table-column>
            <vxe-table-column field="reason" title="申请事由" min-width="200"></vxe-table-column>
            <vxe-table-column field="realityNumber" title="状态" width="120">
              <template #default="{ row }">
                <span>{{ row.completedStatus | CompletedStatus }}</span>
              </template>
            </vxe-table-column>
            <vxe-table-column field="remark" title="备注" min-width="120"></vxe-table-column>
            <vxe-table-column title="操作" width="100" fixed="right" v-if="isOrgRole">
              <template #default="{ row }">
                <span
                  v-if="row.completedStatus === '0' && row.orgId === userInfo.loginPostInfo.orgId"
                  class="el-icon-edit-outline edit-btn"
                  @click="operate(row)"></span>
              </template>
            </vxe-table-column>
          </vxe-table>
        </div>
        <div class="table-pagination">
          <pagination
            class="footer sub-margin"
            :page-size.sync="tablePage.pageSize"
            :total="tablePage.totalSize"
            :current-page.sync="tablePage.pageNum"
            layout="slot,prev, pager, next, sizes, jumper"
            @currentChange="onPageChange" />
        </div>
      </div>
    </div>
    <XnDialog
      title="状态调整"
      :visible.sync="dialogShow"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      width="50%"
      left="10vw"
      class="branch_dialog"
      @closed="dialogShow = false">
      <el-radio-group v-model="applyType">
        <el-radio label="0" border>进行中</el-radio>
        <el-radio label="1" border>已办结</el-radio>
      </el-radio-group>
      <el-input
        type="textarea"
        v-model="suggest"
        placeholder="请输入维修后反馈意见"
        rows="5"
        maxlength="100"
        show-word-limit></el-input>
      <div class="btn_save">
        <el-button class="btnBorder" size="medium" @click="dialogShow = false">取 消</el-button>
        <el-button size="medium" class="btnBg" type="primary" @click="submit">提 交</el-button>
      </div>
    </XnDialog>
  </div>
</template>

<script>
import {
  getStatisticList,
  getOrgList,
  updateCompletedStatus,
  exportExcel,
  getYearMonthList,
} from '@apis/api-dailyManage.js'
import moment from 'moment'
import XnDialog from '@com/xnDialog.vue'
import pagination from '@com/pagination.vue'
import { mapGetters } from 'vuex'
export default {
  name: 'repairStatistics',
  components: { XnDialog, pagination },
  props: {
    userType: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      load: false,
      statusOptions: [
        {
          value: '',
          label: '全部',
        },
        {
          value: '0',
          label: '进行中',
        },
        {
          value: '1',
          label: '已完成',
        },
      ],
      statusData: '',
      yearOption: [],
      yearData: {},
      monthOption: [],
      monthData: moment().format('M'),
      tableData: [],
      userInfo: JSON.parse(localStorage.PSKH_USERINFO),
      tablePage: {
        pageNum: 1,
        pageSize: 10,
        totalSize: 0,
      },
      dialogShow: false,
      applyType: '0',
      suggest: '',
      orgOptions: [],
      orgData: '',
      activeRow: {},
    }
  },
  computed: {
    ...mapGetters('role', ['isOtherRole', 'isNormalRole', 'isOrgRole', 'isRepairRole']),
  },
  filters: {
    CompletedStatus(val) {
      if (val === '0') {
        return '进行中'
      } else if (val === '1') {
        return '已完成'
      } else {
        return ''
      }
    },
  },
  watch: {},
  created() {
    this.getYearMonthList()
  },
  mounted() {},
  methods: {
    getData() {
      if (this.yearData.year === '全部') return
      getStatisticList({
        repairMonth: this.monthData,
        orgId: this.orgData,
        repairYear: this.yearData.year,
        completedStatus: this.statusData,
        current: this.tablePage.pageNum,
        size: this.tablePage.pageSize,
        tenantId: localStorage.pskh_tenantId,
        orgType: '',
      }).then(res => {
        this.load = false
        this.tableData = res.data.records
        this.tablePage.totalSize = res.data.total
      })
    },
    selectTime(type) {
      if (type === 'year') {
        this.monthOption = this.yearData.monthList
        this.monthData = this.monthOption[0].month
      }
      this.tablePage.pageNum = 1
      if (['1', '2'].includes(this.userType)) {
        this.getData()
      } else if (['3'].includes(this.userType)) {
        this.getPreData()
      }
    },
    selectOrg() {
      this.tablePage.pageNum = 1
      this.getData()
    },
    selectStatus() {
      this.tablePage.pageNum = 1
      this.getData()
    },
    onPageChange(e) {
      this.tablePage.pageNum = e
      this.getData()
    },
    operate(row) {
      this.activeRow = row
      this.suggest = this.activeRow.remark || ''
      this.dialogShow = true
    },
    async getPreData() {
      const { data } = await getOrgList({ orgType: '0' })
      const obj = {
        id: '',
        name: '全部',
      }
      this.orgOptions = [obj, ...data]
      this.orgData = ''
      this.getData()
    },
    exportData() {
      if (this.yearData.year === '全部') return
      const req = `?completedStatus=${this.statusData}&orgId=${this.orgData}&orgType=&repairMonth=${this.monthData}&repairYear=${this.yearData.year}&tenantId=${localStorage.pskh_tenantId}`
      const link = document.createElement('a')
      link.target = 'targetName'
      link.href = exportExcel(req)
      link.click()
      link.remove()
    },
    submit() {
      updateCompletedStatus({
        completedStatus: this.applyType,
        id: this.activeRow.num,
        remark: this.suggest,
      }).then(res => {
        this.getData()
        this.dialogShow = false
      })
    },
    getYearMonthList() {
      getYearMonthList({
        type: '1',
      }).then(res => {
        this.yearOption = res.data
        this.yearData = this.yearOption[0]
        this.monthOption = res.data[0].monthList
        this.monthData = this.monthOption[0].month
        if (this.yearData.year !== '全部') {
          if (['1', '2'].includes(this.userType)) {
            this.orgData = this.userInfo.loginPostInfo.orgId
            this.getData()
          } else if (['3'].includes(this.userType)) {
            this.getPreData()
          }
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.repair {
  height: 100%;
  .screen_box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    height: 52px;
    border-bottom: 1px solid #efefef;
    // box-shadow: 0 3px 5px 0 rgba(2, 2, 2, 0.1);
    .select_box {
      display: flex;
      align-items: center;
      .select_li {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .el-select {
          width: 150px;
        }
      }
    }
  }
  .info_con {
    display: flex;
    height: calc(100% - 60px);
    .info_right {
      flex: 1;
      height: 100%;
    }
  }
  .table_con {
    margin: 15px 20px 0;
    height: calc(100% - 50px);
    .edit-btn {
      cursor: pointer;
      font-size: 20px;
    }
  }
  .table-pagination {
    margin: 10px 20px 0;
  }
}
.branch_dialog {
  /deep/.el-dialog__body {
    padding-left: 20px;
    padding-right: 20px;
    .el-radio {
      margin-right: 20px;
    }
    .el-textarea {
      margin: 20px 0;
    }
    .btn_save {
      text-align: right;
    }
  }
}
</style>
